package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 上海资信报告-查询记录&资信提示信息
 */
public class ShangHaiNfcsReportQuerryRecord extends ParseBase {
    private Text key = new Text();
    private Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NfcsReport obj = (NfcsReport) model;
        NfcsData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = new StringBuilder(500);


        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        JSONObject queryInformation = info.getString("queryInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("queryInformation"));
        JSONArray queryRecords = queryInformation.getString("queryRecords") == null ? new JSONArray() : queryInformation.getJSONArray("queryRecords");

        JSONObject nfcsPromptInformation = info.getString("nfcsPromptInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("nfcsPromptInformation"));
        JSONArray nfcsPrompts = nfcsPromptInformation.getString("nfcsPrompt") == null ? new JSONArray() : nfcsPromptInformation.getJSONArray("nfcsPrompt");

        if (!queryRecords.isEmpty()) {
            for (int i = 0; i < queryRecords.size(); i++) {
                JSONObject queryRecord = queryRecords.getString(i) == null ? new JSONObject() : JSON.parseObject(queryRecords.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(queryRecord.getString("queryType") == null ? "" : queryRecord.getString("queryType")).append(CRT001);
                txSb.append(queryRecord.getString("queryReason") == null ? "" : queryRecord.getString("queryReason")).append(CRT001);
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(queryRecord.getString("queryDate") == null ? "" : queryRecord.getString("queryDate")).append(CRT001);
                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }

        if (!nfcsPrompts.isEmpty()) {
            for (int i = 0; i < nfcsPrompts.size(); i++) {
                JSONObject nfcsPrompt = nfcsPrompts.getString(i) == null ? new JSONObject() : JSON.parseObject(nfcsPrompts.getString(i));
                txSb.delete(0, txSb.length());
                txSb.append(CRT001);
                txSb.append(CRT001);
                txSb.append(nfcsPrompt.getString("project") == null ? "" : nfcsPrompt.getString("project")).append(CRT001);
                txSb.append(nfcsPrompt.getString("promptContent") == null ? "" : nfcsPrompt.getString("promptContent")).append(CRT001);
                txSb.append(nfcsPrompt.getString("promptDate") == null ? "" : nfcsPrompt.getString("promptDate")).append(CRT001);
                key.set(getKey());
                value.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nfcs_report_query_inc";
    }

    @Override
    public String getSubType() {
        return ".query";
    }

    @Override
    public String getType() {
        return RecordType.NFCS_REPORT.toString();
    }

    @Override
    public String getComment() {
        return "上海资信报告-查询记录&资信提示信息";
    }
}
